
const {NUMBER_FORMAT_ERROE,NUMBER_ALREADY_EXISTS,NUMBER_CREATE_ERROE,NUMBER_NOT_PRESENT } = require("../constant/err.type");
const { getNumberInfo } = require("../service/number.service");

const validator = async (ctx, next) => {
  try {
    ctx.verifyParams({
      numberName: { type:'string', required: true},
      numberNumber: { type:'string', required: true},
    })
  } catch (error) {
    NUMBER_FORMAT_ERROE.dats = error
    console.error(NUMBER_FORMAT_ERROE.msg, ctx?.request?.body);
    ctx.app.emit("error", NUMBER_FORMAT_ERROE, ctx);
    return
  }
  await next();
};


/**
 * 验证号码名称否存在
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
const verifyNumberName = async (ctx, next) => {
  try {
    const { numberName } = ctx?.request?.body;

    const res = await getNumberInfo({ numberName });
    console.log('res',res)
    if (res) {
      console.error(NUMBER_ALREADY_EXISTS.msg, ctx?.request?.body);
      ctx.app.emit("error", NUMBER_ALREADY_EXISTS, ctx);
      return;
    }
  } catch (error) {
    console.error(NUMBER_CREATE_ERROE.msg, error);
    ctx.app.emit("error", NUMBER_CREATE_ERROE, ctx);
    return;
  }
  await next();
};

module.exports = {
  validator,
  verifyNumberName,
};
